package com.pjj.userservice.aop;

import com.pjj.userservice.utils.TokenUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.ControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 76004
 * @apiNote AOP
 * @date 2023/2/7 14:53
 */
@Component
@Aspect
public class UserAdvice {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private HttpServletRequest request;


    @Pointcut("execution(Object com.pjj.userservice.service.UserService.userLogin(*,*))")
    private void pt(){
    }



    @Around("pt()")
    public Object methodBefore(ProceedingJoinPoint point) throws Throwable {
        Map<String,Object>map = (Map<String, Object>) point.proceed();
        Object[] args = point.getArgs();
        try{
            String token = (String) map.get("token");
            System.out.println("token"+token);
            redisTemplate.opsForValue().set(token, (String) args[0],3, TimeUnit.HOURS);
        }catch (Exception e){
            map.put("code",200);
            map.put("msg",e.toString());
        }


        return map;
    }






}
